package references

import (
	"hi-jvm/instructions/base"
	"hi-jvm/rtda"
)

type PUT_FIELD struct{ base.Index16Instruction }

func (self *PUT_FIELD) Execute(frame *rtda.Frame) {
	field, _ := readStaticClassInfo(self.Index, frame, false)

	slotId := field.SlotId
	stack := frame.OperandStack

	if field.IsInt32() {
		val := stack.PopInt()
		ref := stack.PopRef()
		ref.Fields.SetInt(slotId, val)
		return
	}

	if field.IsFloat() {
		val := stack.PopFloat()
		ref := stack.PopRef()
		ref.Fields.SetFloat(slotId, val)
		return
	}


	if field.IsInt64() {
		val := stack.PopLong()
		ref := stack.PopRef()
		ref.Fields.SetLong(slotId, val)
		return
	}

	if field.IsDouble() {
		val := stack.PopDouble()
		ref := stack.PopRef()
		ref.Fields.SetDouble(slotId, val)
		return
	}
	if field.IsRef() {
		val := stack.PopRef()
		ref := stack.PopRef()
		ref.Fields.SetRef(slotId, val)
		return
	}
	// todo
}